<?php
declare (strict_types = 1);

namespace app\middleware;

use think\facade\Log;

class RequestLog {
    // 请求白名单
    protected $whitePath = [
        'ready',
        'healthz'
    ];

    /**
     * 请求日志中间件
     *
     * @param \think\Request $request
     * @param \Closure       $next
     * @return Response
     */
    public function handle($request, \Closure $next) {
        if (env('APP_DEBUG', false)) {
            $st = microtime(true);
            $resp = $next($request);
            $dt = round(microtime(true) - $st, 6) * 1000;
            $msg = sprintf("[requestLog] %s const: %sms", $request->pathinfo(), $dt);
            Log::notice($msg,['dt' => $dt, 'request' => $request]);
            return $resp;
        }else{
            return $next($request);
        }
    }
}